package cdu.notFound.controller;


import cdu.notFound.model.Goods;
import cdu.notFound.model.User;
import cdu.notFound.service.CollectService;
import cdu.notFound.service.GoodsService;
import cdu.notFound.service.impl.CollectServiceImpl;
import cdu.notFound.service.impl.GoodsServiceImpl;
import cdu.notFound.utils.LoginVerify;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 收藏商品
 */
@WebServlet("/CollectServlet")
public class CollectServlet extends HttpServlet {

    //添加一个物品到收藏夹
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if (LoginVerify.isLogin(request)) {
			User user = (User) request.getSession().getAttribute("loginUser");
			int userId = user.getId();
			int goodsId = Integer.parseInt(request.getParameter("goodsId"));
			CollectService collectService = new CollectServiceImpl();
			GoodsService goodsService = new GoodsServiceImpl();
			Goods goods = goodsService.findById(goodsId);
			if(goods!=null && goods.getStates()==2 && collectService.doCreate(userId, goodsId)){
				response.getWriter().print("success");
			}
			else{
				response.getWriter().print("error");
			}
		} else {
			response.getWriter().print("unLogin");
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
